home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / hack / 3_1_3 / sys / atari / tos.c < prev   
Encoding:
C/C++ Source or Header  |  1993-02-23  |  7.0 KB  |  358 lines

  1. /*    SCCS Id: @(#)tos.c    3.1    90/14/08
  2. /* NetHack may be freely redistributed.  See license for details. */
  3.  
  4. /*
  5.  *  TOS system functions.
  6.  */
  7.  
  8. #define NEED_VARARGS
  9. #include "hack.h"
  10. #include "termcap.h"
  11.  
  12. #ifdef TOS
  13.  
  14. # include <osbind.h>
  15. # ifndef WORD
  16. #  define WORD short        /* 16 bits -- redefine if necessary */
  17. # endif
  18.  
  19. #include <ctype.h>
  20.  
  21. static char NDECL(DOSgetch);
  22. static char NDECL(BIOSgetch);
  23. static void NDECL(init_aline);
  24. char *_a_line;            /* for Line A variables */
  25. # ifdef TEXTCOLOR
  26. boolean colors_changed = FALSE;
  27. # endif
  28.  
  29. int
  30. tgetch()
  31. {
  32.     char ch;
  33.  
  34.     /* BIOSgetch can use the numeric key pad on IBM compatibles. */
  35.     if (flags.BIOS)
  36.         ch = BIOSgetch();
  37.     else
  38.         ch = DOSgetch();
  39.     return ((ch == '\r') ? '\n' : ch);
  40. }
  41.  
  42. /*
  43.  *  Keyboard translation tables.
  44.  */
  45. #define KEYPADLO    0x61
  46. #define KEYPADHI    0x71
  47.  
  48. #define PADKEYS     (KEYPADHI - KEYPADLO + 1)
  49. #define iskeypad(x)    (KEYPADLO <= (x) && (x) <= KEYPADHI)
  50.  
  51. /*
  52.  * Keypad keys are translated to the normal values below.
  53.  * When flags.BIOS is active, shifted keypad keys are translated to the
  54.  *    shift values below.
  55.  */
  56. static const struct pad {
  57.     char normal, shift, cntrl;
  58. } keypad[PADKEYS] = {
  59.             {C('['), 'Q', C('[')},        /* UNDO */
  60.             {'?', '/', '?'},        /* HELP */
  61.             {'(', 'a', '('},        /* ( */
  62.             {')', 'w', ')'},        /* ) */
  63.             {'/', '/', '/'},        /* / */
  64.             {C('p'), '$', C('p')},        /* * */
  65.             {'y', 'Y', C('y')},        /* 7 */
  66.             {'k', 'K', C('k')},        /* 8 */
  67.             {'u', 'U', C('u')},        /* 9 */
  68.             {'h', 'H', C('h')},        /* 4 */
  69.             {'.', '.', '.'},
  70.             {'l', 'L', C('l')},        /* 6 */
  71.             {'b', 'B', C('b')},        /* 1 */
  72.             {'j', 'J', C('j')},        /* 2 */
  73.             {'n', 'N', C('n')},        /* 3 */
  74.             {'i', 'I', C('i')},        /* Ins */
  75.             {'.', ':', ':'}            /* Del */
  76. }, numpad[PADKEYS] = {
  77.             {C('['), 'Q', C('[')}    ,    /* UNDO */
  78.             {'?', '/', '?'},        /* HELP */
  79.             {'(', 'a', '('},        /* ( */
  80.             {')', 'w', ')'},        /* ) */
  81.             {'/', '/', '/'},        /* / */
  82.             {C('p'), '$', C('p')},        /* * */
  83.             {'7', M('7'), '7'},        /* 7 */
  84.             {'8', M('8'), '8'},        /* 8 */
  85.             {'9', M('9'), '9'},        /* 9 */
  86.             {'4', M('4'), '4'},        /* 4 */
  87.             {'.', '.', '.'},        /* 5 */
  88.             {'6', M('6'), '6'},        /* 6 */
  89.             {'1', M('1'), '1'},        /* 1 */
  90.             {'2', M('2'), '2'},        /* 2 */
  91.             {'3', M('3'), '3'},        /* 3 */
  92.             {'i', 'I', C('i')},        /* Ins */
  93.             {'.', ':', ':'}            /* Del */
  94. };
  95.  
  96. /*
  97.  * Unlike Ctrl-letter, the Alt-letter keystrokes have no specific ASCII
  98.  * meaning unless assigned one by a keyboard conversion table, so the
  99.  * keyboard BIOS normally does not return a character code when Alt-letter
  100.  * is pressed.    So, to interpret unassigned Alt-letters, we must use a
  101.  * scan code table to translate the scan code into a letter, then set the
  102.  * "meta" bit for it.  -3.
  103.  */
  104. #define SCANLO        0x10
  105.  
  106. static const char scanmap[] = {     /* ... */
  107.     'q','w','e','r','t','y','u','i','o','p','[',']', '\n',
  108.     0, 'a','s','d','f','g','h','j','k','l',';','\'', '`',
  109.     0, '\\', 'z','x','c','v','b','N','m',',','.','?'     /* ... */
  110. };
  111.  
  112. #define inmap(x)    (SCANLO <= (x) && (x) < SCANLO + SIZE(scanmap))
  113.  
  114. /*
  115.  * BIOSgetch gets keys directly with a BIOS call.
  116.  */
  117. #define SHIFT        (0x1 | 0x2)
  118. #define CTRL        0x4
  119. #define ALT        0x8
  120.  
  121. static char
  122. BIOSgetch()
  123. {
  124.     unsigned char scan, shift, ch;
  125.     const struct pad *kpad;
  126.  
  127.     long  x;
  128.  
  129.     /* Get scan code.
  130.      */
  131.     x = Crawcin();
  132.     ch = x & 0x0ff;
  133.     scan = (x & 0x00ff0000L) >> 16;
  134.     /* Get shift status.
  135.      */
  136.     shift = Kbshift(-1);
  137.  
  138.     /* Translate keypad keys */
  139.     if (iskeypad(scan)) {
  140.         kpad = flags.num_pad ? numpad : keypad;
  141.         if (shift & SHIFT)
  142.             ch = kpad[scan - KEYPADLO].shift;
  143.         else if (shift & CTRL)
  144.             ch = kpad[scan - KEYPADLO].cntrl;
  145.         else
  146.             ch = kpad[scan - KEYPADLO].normal;
  147.     }
  148.     /* Translate unassigned Alt-letters */
  149.     if ((shift & ALT) && !ch) {
  150.         if (inmap(scan))
  151.             ch = scanmap[scan - SCANLO];
  152.         return (isprint(ch) ? M(ch) : ch);
  153.     }
  154.     return ch;
  155. }
  156.  
  157. static char
  158. DOSgetch()
  159. {
  160.     return (Crawcin() & 0x007f);
  161. }
  162.  
  163.  
  164. long
  165. freediskspace(path)
  166. char *path;
  167. {
  168.     int drive = 0;
  169.     struct {
  170.         long freal; /*free allocation units*/
  171.         long total; /*total number of allocation units*/
  172.         long bps;   /*bytes per sector*/
  173.         long pspal; /*physical sectors per allocation unit*/
  174.     } freespace;
  175.     if (path[0] && path[1] == ':')
  176.         drive = (toupper(path[0]) - 'A') + 1;
  177.     if (Dfree(&freespace,drive)<0) return -1;
  178.     return freespace.freal*freespace.bps*freespace.pspal;
  179. }
  180.  
  181. /*
  182.  * Functions to get filenames using wildcards
  183.  */
  184. int
  185. findfirst(path)
  186. char *path;
  187. {
  188.     return (Fsfirst(path, 0) == 0);
  189. }
  190.  
  191. int
  192. findnext()
  193. {
  194.     return (Fsnext() == 0);
  195. }
  196.  
  197. char *
  198. foundfile_buffer()
  199. {
  200.     return (char *)Fgetdta() + 30;
  201. }
  202.  
  203. long
  204. filesize(file)
  205. char *file;
  206. {
  207.     if (findfirst(file))
  208.         return  (* (long *) ((char *)Fgetdta() + 26));
  209.     else
  210.         return -1L;
  211. }
  212.  
  213. /*
  214.  * Chdrive() changes the default drive.
  215.  */
  216. void
  217. chdrive(str)
  218. char *str;
  219. {
  220.     char *ptr;
  221.     char drive;
  222.  
  223.     if ((ptr = index(str, ':')) != NULL) {
  224.         drive = toupper(*(ptr - 1));
  225.         (void)Dsetdrv(drive - 'A');
  226.     }
  227.     return;
  228. }
  229.  
  230.  
  231. void
  232. get_scr_size()
  233. {
  234. # ifdef MINT
  235. #  include <ioctl.h>
  236.     struct winsize win;
  237.  
  238.     ioctl(0,TIOCGWINSZ, &win);
  239.     LI = win.ws_row;
  240.     CO = win.ws_col;
  241. # else
  242.     init_aline();
  243.     LI = (*((WORD  *)(_a_line + -42L))) + 1;
  244.     CO = (*((WORD  *)(_a_line + -44L))) + 1;
  245. # endif
  246. }
  247.  
  248. # define BIGBUF  8192
  249.  
  250. int
  251. _copyfile(from, to)
  252. char *from, *to;
  253. {
  254.     int fromfd, tofd, r;
  255.     char *buf;
  256.  
  257.     if ((fromfd = open(from, O_RDONLY|O_BINARY, 0)) < 0)
  258.         return -1;
  259.     if ((tofd = open(to, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, FCMASK)) < 0)
  260.         return -1;
  261.     buf = (char *)alloc((size_t)BIGBUF);
  262.     while ( (r = read(fromfd, buf, BIGBUF)) > 0)
  263.         write(tofd, buf, r);
  264.     close(fromfd);
  265.     close(tofd);
  266.     free(buf);
  267.     return 0;    /* successful */
  268. }
  269.  
  270. int kbhit()
  271. {
  272.     return Cconis();
  273. }
  274.  
  275. static void
  276. init_aline()
  277. {
  278. # ifdef __GNUC__
  279. /* line A calls nuke registers d0-d2,a0-a2; not all compilers regard these
  280.    as scratch registers, though, so we save them
  281.  */
  282.     asm(" moveml d0-d2/a0-a2, sp@-");
  283.     asm(" .word 0xa000; movel d0, __a_line");
  284.     asm(" moveml sp@+, d0-d2/a0-a2");
  285. # else
  286.     asm(" movem.l d0-d2/a0-a2, -(sp)");
  287.     asm(" .dc.w 0xa000");    /* tweak as necessary for your compiler */
  288.     asm(" move.l d0, __a_line");
  289.     asm(" movem.l (sp)+, d0-d2/a0-a2");
  290. # endif
  291. }
  292.  
  293. # ifdef TEXTCOLOR
  294. /* used in termcap.c to decide how to set up the hilites */
  295. unsigned long tos_numcolors = 2;
  296.  
  297. void
  298. set_colors()
  299. {
  300.     static char colorHE[] = "\033q\033b0";
  301.  
  302.     if (!flags.BIOS)
  303.         return;
  304.     init_aline();
  305.     tos_numcolors = 1 << (((unsigned char *) _a_line)[1]);
  306.     if (tos_numcolors <= 2) {            /* mono */
  307.         flags.use_color = FALSE;
  308.         return;
  309.     } else {
  310.         colors_changed = TRUE;
  311.         HE = colorHE;
  312.     }
  313. }
  314.  
  315. void
  316. restore_colors()
  317. {
  318.     static char plainHE[] = "\033q";
  319.  
  320.     if (colors_changed)
  321.         HE = plainHE;
  322.     colors_changed = FALSE;
  323. }
  324. # endif /* TEXTCOLOR */
  325.  
  326. # ifdef SUSPEND
  327.  
  328. #include    <signal.h>
  329.  
  330. #   ifdef MINT
  331. extern int __mint;
  332. #   endif
  333.  
  334. int
  335. dosuspend() {
  336. #   ifdef MINT
  337.     extern int kill();
  338.     if (__mint == 0) {
  339. #   endif
  340.         pline("Sorry, it seems we have no SIGTSTP here.  Try ! or S.");
  341. #   ifdef MINT
  342.     }
  343.     else if(signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
  344.         suspend_nhwindows(NULL);
  345.         (void) signal(SIGTSTP, SIG_DFL);
  346.         (void) kill(0, SIGTSTP);
  347.         get_scr_size();
  348.         resume_nhwindows();
  349.     } else {
  350.         pline("I don't think your shell has job control.");
  351.     }
  352. #   endif /* MINT */
  353.     return(0);
  354. }
  355. # endif /* SUSPEND */
  356.  
  357. #endif /* TOS */
  358.